
package com.hexinfo.dmpro.common.service.impl;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hexinfo.dmpro.common.model.MessageCenter;
import com.hexinfo.dmpro.common.dao.MessageCenterMapper;
import com.hexinfo.dmpro.common.service.MessageCenterService;
import com.hexinfo.dmpro.common.utils.ScanCommonConstants;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Map;

/**
 * 消息中心表
 *
 * @author yemw
 * @date 2023-08-29 17:55:32
 */
@Service
public class MessageCenterServiceImpl extends ServiceImpl<MessageCenterMapper, MessageCenter> implements MessageCenterService {

    @Override
    public Page queryList(Page page, MessageCenter messageCenter) {
        LambdaQueryWrapper<MessageCenter> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotBlank(messageCenter.getMenu()),MessageCenter::getMenu,messageCenter.getMenu())
                .like(StrUtil.isNotBlank(messageCenter.getOperator()),MessageCenter::getOperator,messageCenter.getOperator())
                .like(StrUtil.isNotBlank(messageCenter.getMessageSubject()),MessageCenter::getMessageSubject,messageCenter.getMessageSubject())
                .eq(StrUtil.isNotBlank(messageCenter.getDataType()),MessageCenter::getDataType,messageCenter.getDataType())
                .eq(MessageCenter::getDel,"0")
                .apply(StrUtil.isNotBlank(messageCenter.getStaDate()),"CREATE_TIME between to_date('" + messageCenter.getStaDate() + "','yyyy-MM-dd HH24:MI:SS') and to_date('"+ messageCenter.getEndDate() + "','yyyy-MM-dd HH24:MI:SS')")
                .orderByDesc(MessageCenter::getCreateTime)
        ;
        Page returnPage = this.baseMapper.selectPage(page, wrapper);
        return returnPage;
    }

    /**
     * 获取最新一条数据
     * @param message 消息主题
     * @return
     */
    @Override
    public MessageCenter orderToCreateTimeONE(String message) {
        return this.baseMapper.orderToCreateTimeONE(message);
    }


    /**
     * 下载日志信息
     * @param request
     * @param response
     * @throws Exception
     */
    @Override
    public void downLoadLog(HttpServletRequest request, HttpServletResponse response,String logPath) throws IOException {
        File file = FileUtil.file(logPath);
        String fileName = FileUtil.getName(file);
        //设置响应头
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        //输出文件内容
        ServletOutputStream out = response.getOutputStream();
        out.write(FileUtil.readBytes(file));
        out.flush();

        //关闭流
        out.close();
    }


    /**
     * 读取日志信息
     * @throws Exception
     */
    @Override
    public String readLoadLog(String logPath){
        try {
            byte[] sb = FileUtil.readBytes(new File(logPath));
            ByteArrayInputStream byteArrayInputStream = IoUtil.toStream(sb);
            String str = IoUtil.read(byteArrayInputStream, StandardCharsets.UTF_8);
            return str;
        } catch (IORuntimeException e) {
            return logPath+ScanCommonConstants.ClusterName.LJERR;
        }

    }

    /**
     * 按主题获取热备最进时间
     * @throws Exception
     */
    @Override
    public Date maxCreateTime(String message){
        return this.baseMapper.maxCreateTime(message);
    }

}
